package com.lanswon.qzsmk.controller.systemManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.controller.cardManageController.BaseController;
import com.lanswon.qzsmk.dao.CardTypeMapper;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.CardProdunctDefinition;
import com.lanswon.qzsmk.model.CardType;
import com.lanswon.qzsmk.service.cardManageService.ApplyCardService;
import com.lanswon.qzsmk.service.systemManageService.CardParameterManagementService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.BusiParamVo;
import com.lanswon.qzsmk.vo.CardProdunctVo;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultVo;

/**
 * 新增履历
 * 工程项目名称：青州市民卡核心系统
 * 方法名：卡参数管理-CardProdunctController
 * 版本： ver 1.0  初版
 * 创建日期：20190319
 * 创建人：鱼洲杰
 */
@Controller
@RequestMapping(value="/parameterCard")
public class CardProdunctController {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(CardProdunctController.class);
	
	@Autowired
	ApplyCardService acs;
	
	@Autowired
	CardParameterManagementService cpm;
	
	@Autowired
	ParamManageService pm;

	@Autowired
	CardTypeMapper ctm;
	

	/**
	 * 跳转卡产品页面
	 * @return
	 */
	@RequiresPermissions("cardProductDefinition:index")
	@RequestMapping(name="卡产品定义主页,cardProductDefinition:index",value="/queryCardProductDefinition",method={RequestMethod.GET,RequestMethod.POST})
	public String CardBatch(){
		logger.info("CardBatch########start()");
		logger.info("CardBatch########end()");
		return "CardProductDefinition";
	}

	/**
	 * 动态获取卡物理类型、卡产品集合、证件类型集合
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardProductDefinition:getCardInfo")
	@RequestMapping(name="卡产品定义获取卡信息,cardProductDefinition:getCardInfo",value="/getCardInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>getCardInfo(HttpServletRequest request) throws Exception{
		logger.info("getCardInfo########start()");
		List<CardProdunctDefinition> cdProDefinitions=null;
		ResultVo resultVo=cpm.queryAllCardProdunctDefinition();//卡产品集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdProDefinitions=(List<CardProdunctDefinition>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		/*获取卡类型：记名，不记名，标准*/
		List<CardType> cardTypeList = cpm.queryAllCardType();
		Map<String, Object> map=new HashMap<>();
		map.put("cdProDefinitions", cdProDefinitions);
		map.put("cardTypes", cardTypeList);
		logger.info("getCardInfo########end()");
		return map;
	}
	/**
	 * 新增页面动态获取卡类型代码
	 * @param request
	 * @return
	 */
	@RequiresPermissions("cardProductDefinition:getCardType")
	@RequestMapping(name="卡产品定义获取卡类型代码,cardProductDefinition:getCardType",value="/getCardType",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>getCardType(HttpServletRequest request){
		logger.info("getCardType########start()");
		List<CardType> cardType=cpm.queryAllCardType();//卡类型代码集合
		logger.debug("cardType====>"+cardType.toString());
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		Map<String, Object> map=new HashMap<>();
		map.put("CardType", cardType);
		map.put("activeUser", activeUser);
		logger.info("getCardType########end()");
		return map;
	}
	/**
	 * 获取卡产品信息
	 * @param cpv
	 * @param pageCount
	 * @throws Exception 
	 */
	@RequiresPermissions("cardProductDefinition:getCardProductD")
	@RequestMapping(name="卡产品定义获取卡产品信息,cardProductDefinition:getCardProductD",value="/getCardProductD",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<CardProdunctVo> getCardProductD(CardProdunctVo cpv,Integer pageNum,Integer numPerPage,HttpServletRequest request) throws Exception{
		logger.info("getCardProductD########start()");
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		cpv.setCurrentPage(pageNum);
		cpv.setNumPerPage(numPerPage);
		cpv.setStartLine((cpv.getCurrentPage()-1)*cpv.getNumPerPage());
		DwzPage<CardProdunctVo> dPage=null;
		ResultVo resultVo=cpm.queryAllCardProdunct(cpv);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
					dPage=(DwzPage<CardProdunctVo>) resultVo.getServiceObject();
		}
		
		logger.info("getCardProductD########end()");
		return dPage;
	}
	/**
	 * 去新增页面
	 * @return
	 */
	@RequiresPermissions("cardProductDefinition:addCardProduct")
	@RequestMapping(name="卡产品定义新增页面,cardProductDefinition:addCardProduct",value="/addCardProduct",method=RequestMethod.GET)
	public String addproduct(HttpServletRequest request){
		logger.info("addproduct########end()");
		logger.info("addproduct########end()");
		return "AddCardProduct";
	}
	/**
	 * 去修改页面
	 * @return
	 */
	@RequiresPermissions("cardProductDefinition:updateCardProduct")
	@RequestMapping(name="卡产品定义修改页面,cardProductDefinition:updateCardProduct",value="/updateCardProduct",method=RequestMethod.GET)
	public String updateProduct(HttpServletRequest request){
		logger.info("updateProduct########end()");
		request.getSession().setAttribute("sign", true);
		logger.info("updateProduct########end()");
		return "UpdateCardProduct";
	}
	/**
	 * 新增卡产品，并刷新页面
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardProductDefinition:addCardp")
	@RequestMapping(name="卡产品定义新增卡产品,cardProductDefinition:addCardp",value="/addCardp",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addCardp(CardProdunctVo cpv,String cardpCode) throws Exception{
		logger.info("addProduct########start()");
		Map<String,Object> map=new HashMap<String,Object>();
		
			CardProdunctDefinition cpv1=cpm.queryCardProductByCode(cpv.getCardpCode());
			CardProdunctDefinition cpv2=cpm.queryCardProductByName(cpv.getCardpName());
			logger.info("oooooooooo"+cpv1);
			if(cpv1==null&&cpv2==null){
				cpm.AddProductTask(cpv);
				BusiParamVo bpv=new BusiParamVo();
				bpv.setCardpCode(cpv.getCardpCode());
				//bpv.setCardpName(cpv.getCardpName());
				cpm.addBusPara(bpv);
				map.put("statusCode", "200");
				map.put("message", "操作成功");
				map.put("navTabId", "productCard");
				map.put("callbackType", "closeCurrent");
				map.put("forwardUrl", "parameterCard/queryCardProductDefinition");//post请求
			}else if(cpv1!=null){
				map.put("statusCode", "300");
				map.put("message", "已有该卡产品代码");
			}else if(cpv2!=null){
				map.put("statusCode", "300");
				map.put("message", "已有该卡产品名称");
			}
		logger.info("addProduct########end()");
		return map;
	}

	/**
	 * 去修改页面
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardProductDefinition:goUpdateProductTask")
	@RequestMapping(name="卡产品定义去修改页面,cardProductDefinition:goUpdateProductTask",value="/goUpdateProductTask",method=RequestMethod.GET)
	public String updateProductTask(String param,HttpServletRequest request) throws Exception{
		logger.info("updateProductTask########start()");
		logger.debug("前画面param###=>"+param);
		
		CardProdunctDefinition cpv= pm.queryCardPCode(param);
		CardType cardTypeObject = ctm.queryCardTypeByCode(cpv.getCardTypeCode());
		//request.getSession().setAttribute("sign", false);
		request.getSession().setAttribute("cpv", cpv);
		request.getSession().setAttribute("cardType", cardTypeObject);
		logger.info("updateProductTask########end()");
		return "UpdateCardProduct";
	}
	/**
	 * 获取要修改的信息
	 * @param 
	 * @return
	 */
	@RequiresPermissions("cardProductDefinition:getUpdateProduct")
	@RequestMapping(name="卡产品定义获取修改信息,cardProductDefinition:getUpdateProduct",value="/getUpdateProduct",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUpdateInfo(HttpServletRequest request){
		logger.info("getUpdateInfo########start()");
		Map<String, Object> map=new HashMap<>();
		map.put("cpv", request.getSession().getAttribute("cpv"));
		map.put("cardType", request.getSession().getAttribute("cardType"));
		request.getSession().removeAttribute("cpv");
		request.getSession().removeAttribute("cardType");
		logger.info("getUpdateInfo########end()");
		return map;
	}
	/**
	 * 修改卡产品，并刷新页面
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardProductDefinition:updateProduct")
	@RequestMapping(name="卡产品定义修改卡产品,cardProductDefinition:updateProduct",value="/updateProduct",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateProduct(CardProdunctVo cpv,HttpServletRequest request) throws Exception{
		logger.info("updateProduct########start()");
		Map<String,Object> map=new HashMap<String,Object>();
		cpm.modifyCardTaskByCityCode(cpv);
		logger.debug("cardpCode====>"+cpv.getCardpCode());
		logger.debug("cardTypeCode====>"+cpv.getDescription());
		map.put("statusCode", "200");
		map.put("message", "操作成功");
		map.put("navTabId", "productCard");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "parameterCard/queryCardProductDefinition");//post请求
		logger.info("updateProduct########end()");
		return map;
	}
	/**
	 * 启用或者停用卡产品
	 * @return
	 * @throws Exception 
	
	 */
	@RequiresPermissions("cardProductDefinition:startProduct")
	@RequestMapping(name="卡产品定义启用卡产品,cardProductDefinition:startProduct",value="/startProduct",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> startProduct(String param) throws Exception{
			logger.info("startProduct#########start()");
			Map<String,Object> map=new HashMap<String,Object>();
			logger.debug("id====>"+param);
			CardProdunctDefinition cd=cpm.queryCardProductByID(param);

				cpm.startProduct(cd);
				map.put("statusCode", "200");
				map.put("message", "操作成功");
			logger.info("startProduct#########end()");
			return map;
		}
		
	
	/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+++++++++++++++++++++++卡业务参数++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

	/**
	 * 跳转业务参数页面
	 * @return
	 */
	@RequiresPermissions("cardBusinessParameters:index")
	@RequestMapping(name="卡业务参数主页,cardBusinessParameters:index",value="/queryCardBusinessParameters",method={RequestMethod.POST,RequestMethod.GET})
	public String Card(){
		return "CardBusinessParameters";
	}
	/**
	 * 获取卡产品信息
	 * @param lci
	 * @param pageCount
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("cardBusinessParameters:getCardBusiness")
	@RequestMapping(name="卡业务参数获取卡产品信息,cardBusinessParameters:getCardBusiness",value="/getCardBusiness",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<BusiParamVo> getCardBusiness(BusiParamVo bpv,Integer pageNum,Integer numPerPage,HttpServletRequest request) throws Exception{
		logger.info("getCardBusiness########start()");
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		bpv.setCurrentPage(pageNum);
		bpv.setNumPerPage(numPerPage);
		bpv.setStartLine((bpv.getCurrentPage()-1)*bpv.getNumPerPage());
		DwzPage<BusiParamVo> dPage=null;
				cpm.queryAllBusiParam(bpv);
		ResultVo resultVo=cpm.queryAllBusiParam(bpv);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
					dPage=(DwzPage<BusiParamVo>) resultVo.getServiceObject();
		}		
		logger.info("getCardBusiness########end()");
		return dPage;
	}
	/**
	 * 更新卡产品，并刷新页面
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardBusinessParameters:updateBusPara")
	@RequestMapping(name="卡业务参数新增卡产品,cardBusinessParameters:updateBusPara",value="/updateBusPara",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateBusPara(BusiParamVo bpv,String cardpCode1,HttpServletRequest request) throws Exception{
		logger.info("updateBusPara########start()");
		//编辑对话框出啊过来的卡产品查询值
		String  cardPCodeQue = request.getParameter("cardPCodeByQue");
		logger.debug("编辑对话框传过来的卡产品查询值==>"+cardPCodeQue);
		Map<String,Object> map=new HashMap<String,Object>();			
			cpm.modifyCardBusParaByCityCode(bpv);
			//20180319 惠国磊  以下  行  增加
			map.put("cardPCodeByUpdate", cardPCodeQue);
			map.put("statusCode", "200");
			map.put("message", "操作成功");
			map.put("navTabId", "businessCard");
			map.put("callbackType", "closeCurrent");
			map.put("forwardUrl", "parameterCard/queryCardBusinessParameters");//post请求	
		
		
			logger.info("updateBusPara########end()");
		return map;
	
	}
	
	/**
	 * 去修改页面
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("cardBusinessParameters:goupdateBusiParamTask")
	@RequestMapping(name="卡业务参数去修改页面,cardBusinessParameters:goupdateBusiParamTask",value="/goupdateBusiParamTask",method=RequestMethod.GET)
	public String goupdateBusiParamTask( String param,HttpServletRequest request) throws Exception{
		logger.info("goupdateBusiParamTask########start()");
		//前画面查询条件值：卡产品代码
		String cardPCodePm = request.getParameter("cardPCodeByQue");
		logger.debug("前画面查询条件值-卡产品代码==>"+cardPCodePm);
		BusiParamVo bp= cpm.queryCardBusiParamByCode(param);
		request.getSession().setAttribute("bp", bp);
		request.getSession().setAttribute("cardPCodeByQue", cardPCodePm);
		logger.info("goupdateBusiParamTask########end()");
		return "UpdateBusinessParameters";
	}
	/**
	 * 获取要修改的信息
	 * @param 
	 * @return
	 */
	@RequiresPermissions("cardBusinessParameters:getUpdateBusiParamInfo")
	@RequestMapping(name="卡业务参数获取修改信息,cardBusinessParameters:getUpdateBusiParamInfo",value="/getUpdateBusiParamInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUpdateBusiParamInfo(HttpServletRequest request){
		logger.info("getUpdateBusiParamInfo########start()");
		Map<String, Object> map=new HashMap<>();
		//map.put("sign", request.getSession().getAttribute("sign"));
		map.put("bp", request.getSession().getAttribute("bp"));
		map.put("cardPCodeByQue", request.getSession().getAttribute("cardPCodeByQue"));
		request.getSession().removeAttribute("cardPCodeByQue");
		request.getSession().removeAttribute("bp");
		logger.info("getUpdateBusiParamInfo########end()");
		return map;
	}
}
